﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using SIGERP_Entidades;
using SIGERP_Negocio;

using System.Data.SqlClient;

namespace SIGERP_Presentacion
{
    public partial class frmArea : frmABM
    {
        List<Area> areas;
        DataTable dt;

        public  frmArea()
        {
            base.escribeLabels("Área");
            base.CargarGrilla();
        }

        public override void CargarGrilla()
        {

            dt = new DataTable();
            dt.Columns.Add("Id", typeof(int));
            dt.Columns.Add("Área", typeof(string));
            dt.Columns.Add("Habilitado", typeof(bool));
            
            areas = Areas.buscarAreas();
            foreach(Area a in areas)
            dt.Rows.Add(a.id, a.descripcion,a.habilitado);
           
            base.Grilla.DataSource = dt;
            base.Grilla.Columns[0].Visible = false;
        }

        public override void busqueda()
        {
            dt.Clear();

            foreach (Area a in areas)
            {

                if (a.descripcion.ToUpper().StartsWith(txtPais.Text.ToUpper()))
                    dt.Rows.Add(a.id, a.descripcion, a.habilitado);
            }
        }

        public override void agregar()
        {
            frmAreaNueva frm = new frmAreaNueva();
            DialogResult opc = frm.ShowDialog(); 
            frm.Dispose();
            CargarGrilla();
        }

        public override void eliminar()
        {
            if (base.Grilla.SelectedRows.Count == 1)
            {
                if (MessageBox.Show("¿Desea dar de baja el área seleccionada?", Text, MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
                {
                    Area a = Areas.buscarArea(Convert.ToInt32(base.Grilla.SelectedRows[0].Cells["Id"].Value));
                    Areas.borrar(a);
                    CargarGrilla();
                }
            }
        }


        public override void editar()
        {
            if (base.Grilla.SelectedRows.Count == 1)
            {
                Area a = new Area();
                a.id = Convert.ToInt32(base.Grilla.SelectedRows[0].Cells["Id"].Value);
                a.descripcion = base.Grilla.SelectedRows[0].Cells["Área"].Value.ToString();
                a.habilitado = Convert.ToBoolean(base.Grilla.SelectedRows[0].Cells["Habilitado"].Value);
                frmAreaActualizar frm = new frmAreaActualizar(a);
                DialogResult opc = frm.ShowDialog();     
                frm.Dispose();
                CargarGrilla();
            }
        }

    }
}